package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2024/7/30 09:35
 * @apiNote
 */

public class Test00050MyPow {

    public double myPow(double x, int n) {
        long N = n;
        if (N < 0) {
            x = 1 / x;
            N = -N;
        }
        double ans = 1.0;
        for(long i = 0; i<N;i++){
            ans = ans*x;
        }
        return ans;
    }
    public double myPow1(double x, int n) {
        long N = n;
        return N >= 0 ? quickMul(x, N) : quickMul(1.0/x, -N);
    }

    public double quickMul(double x, long N){
        if(N == 0){
            return 1.0;
        }
        double y = quickMul(x,N/2);
        return N%2 == 0 ? y*y : y*y*x;
    }

    public double myPow2(double x, int n) {
        long N = n;
        return N >= 0 ? quickMul2(x, N) : quickMul2(1.0/x, -N);
    }

    public double quickMul2(double x, long N){
        double ans = 1.0;
        double xContribute = x;
        while(N > 0){
            if(N%2 == 1){
                ans *= xContribute;
            }
            xContribute *= xContribute;
            N/=2;
        }
        return ans;
    }
}
